package com.gwt.tirso.gymkhana.server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import com.google.gwt.user.client.rpc.IsSerializable;
import com.gwt.tirso.gymkhana.client.Gymkhana;
/*


public class JDBCGymkhanaDAO implements IGymkhanaDAO, IsSerializable {

	private Connection c;

	public JDBCGymkhanaDAO() {
		c = ConnectionManager.getInstance().checkOut();
	}

	protected void finalize() {
		ConnectionManager.getInstance().checkIn(c);
	}

	@Override
	public void insertGymkhana(Gymkhana g) {

		PreparedStatement stmt = null;
		String sql = "INSERT INTO Gymkhana ( id, name, nquestions, nmaxteams, idauthor, description, rules, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			stmt = c.prepareStatement(sql);
			stmt.setString(1, g.getId());
			stmt.setString(2, g.getName());
			stmt.setLong(3, g.getnQuestions());
			stmt.setLong(4, g.getnMaxTeams());
			stmt.setString(5, g.getIdAuthor());
			stmt.setString(6, g.getDescription());
			stmt.setString(7, g.getRules());
			stmt.setString(8, g.getPass());

			stmt.executeUpdate();

		} catch (Exception e) {
			System.out.println("Message: insertGymkhana: " + e.getMessage());
			System.out.println("SQLState: " + ((SQLException) e).getSQLState());
			System.out.println("ErrorCode: "
					+ ((SQLException) e).getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
	}

	@Override
	public Gymkhana selectGymkhanaByNameAndPass(String name, String pass) {

		Connection conn = ConnectionManager.getInstance().checkOut();

		PreparedStatement stmt = null;
		Gymkhana g = new Gymkhana();
		ResultSet result = null;

		try {

			String sql = "SELECT * FROM Gymkhana WHERE name = ? AND password = ?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, name);
			stmt.setString(2, pass);

			stmt.executeQuery();
			result = stmt.executeQuery();

			result.next();
			g.setId(result.getString("id"));
			g.setName(result.getString("name"));
			g.setPass(result.getString("password"));
			g.setRules(result.getString("rules"));
			g.setDescription(result.getString("description"));
			g.setnMaxTeams(result.getInt("nmaxteams"));
			g.setnQuestions(result.getInt("nquestions"));
			g.setIdAuthor(result.getString("idauthor"));

		} catch (SQLException e) {
			System.out.println("Message: selectGymkhanaByNameAndPass: "
					+ e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return g;
	}
}*/
